<?php

namespace App\Http\Controllers\login;

use App\Http\Models\User;
use App\Http\Util\Image;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
   /*登陆*/
    public function CheckIsLogin(Request $request)
    {
        //正则匹配用户输入的是邮箱还是手机号
        $phone = $request->input('phone');
         $password = $request->input('password');
         if(empty($phone)){
             $data = ['code'=>5001,'messages'=>"phone字段不能为空"];
             return response()->json($data);
         }
         if(empty($password)){
             $data = ['code'=>5001,'messages'=>"password字段不能为空"];
             return response()->json($data);
         }
         $userlist = User::where('phone',$phone)->where("status",1)->first();
         if(empty($userlist)){
             $data = ['code'=>5004,'messages'=>"无此用户或手机号未注册"];
             return response()->json($data);
         }
         $status = $userlist->status;
         if($status=="0"){
             $data = ['code'=>5007,'messages'=>"该账号被禁用"];
             return response()->json($data);
         }
         $upass = $userlist->password;
         if(!Hash::check($password,$upass)){
             $data = ['code'=>5005,'messages'=>"密码错误"];
             return response()->json($data);
         }else{
             $avator="image/".$userlist->avator;
             if(@fopen($avator, 'r')) {
                 $userlist->avator=$avator;
             }else{
                 $userlist->avator='/img/181509094162_.pic_hd.jpg';
             }
             if (empty($userlist->email)){
                 $userlist->email = '';
             }
             $user_list = [
                 'id' =>$userlist->id,
                 'avator' => env('APP_WEB_PATH').$userlist->avator,
                 'name' => $userlist->name,
                 'password' => $userlist->password,
                 'email' => $userlist->email,
                 'phone' => $userlist->phone,
                 'account' => $userlist->account,
                 'phone' => $userlist->phone,
                 'leve' => $userlist->leve,
                 'status' => $userlist->status,
             ];
             $data = ['code'=>200,'messages'=>"登陆成功！",'result'=>$user_list];
             return response()->json($data);
         }
    }
   /*用户注册*/

    public function IsUserPhoneRegister(Request $request)
    {
        $input = Input::all();
        $phone = trim($input['phone']);
        $password = trim($input['password']);
        $repassword = trim($input['password_confirmation']);
        $mscode = trim($input['mscode']);
        if(empty($phone)){
            $data = ['code'=>5001,'messages'=>"phone字段不能为空"];
            return response()->json($data);
        }
        if(empty($password)){
            $data = ['code'=>5001,'messages'=>"password字段不能为空"];
            return response()->json($data);
        }
        if($password != $repassword){
            $data = ['code'=>5002,'messages'=>"两次输入的密码不一致！"];
            return response()->json($data);
        }
        $userlist = User::where('phone',$phone)->where('status',1)->first();
        if(!empty($userlist)){
            $data = ['code'=>5000,'messages'=>"该手机号已注册"];
            return response()->json($data);
        }

        if(strlen($password) < 6){
            $data = ['code'=>5006,'messages'=>"新密码长度不能小于6位"];
            return response()->json($data);
        }
//        /**d短信验证码校验*/
//        if(isset($_COOKIE['phonemsg'])){
//            if($mscode != $_COOKIE['phonemsg']){
//                $data = ['code' => 207,'messages' => '验证码错误请重新获取!'];
//                return json_encode($data);
//            }
//        }else{
//            $data= ['code' => 208,'messages' => '验证码过期请重新发送!'];
//            return json_encode($data);
//        }
        /**验证注册手机号是否在系统可用状态*/
        $usersql = DB::table('frontusers')
        ->where(function ($query) use ($phone){
            $query->where('phone',$phone);
        })->first();
        if(!empty($usersql)){
            return $data= ['code' => 206,'messages' => '手机号已存在!'];
        }
        $id = DB::table('frontusers')->insertGetId([
            'name'=>$phone,
            'phone' => $phone,
            'leve' => '1',
            'password' => Hash::make($password),
            'status' => 1,
            'created_at' => date('Y-m-d H:i:s',time()),
            'updated_at' => date('Y-m-d H:i:s',time())
        ]);
        if(empty($id)){
            $data = ['code'=>5008,'messages'=>"注册失败"];
            return response()->json($data);
        }else{
            $data = ['code'=>200,'messages'=>'注册成功！'];
            return response()->json($data);
        }
    }
        /**验证码提取*/
        public function code()
        {
            $code = new Image();
            return $code->buildImageVerify();
        }
}
